package com.mhl.campuscabinet;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.mhl.client.OkHttpUtils;
import com.mhl.entity.LocalData;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.Response;

//todo yuanjq
public class RegisterActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        ImageButton register = findViewById(R.id.register_btn);


        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = ((EditText) findViewById(R.id.account_text_r)).getText().toString().trim();
                String password = ((EditText) findViewById(R.id.password_text_r)).getText().toString().trim();
                String confirmPassword = ((EditText) findViewById(R.id.password_text_r2)).getText().toString().trim();

                register(username,password,confirmPassword);
            }
        });
    }


    private void register(String username,String password,String confirmPassword){
        if (username.equals("") || password.equals("") || confirmPassword.equals(""))
            Toast.makeText(RegisterActivity.this, "Inputs can't be empty", Toast.LENGTH_SHORT).show();
        else if(!password.equals(confirmPassword)){
            Toast.makeText(RegisterActivity.this, "Two passwords do not match", Toast.LENGTH_SHORT).show();
            System.out.println("password: " + password +" confirm:" + confirmPassword);
        }
        else{
            JSONObject jsonParam = new JSONObject();
            try {
                jsonParam.put("username", username);
                jsonParam.put("password", password);
                String json = jsonParam.toString();
                MediaType mediaType = MediaType.Companion.parse("application/json;charset=utf-8");
                RequestBody requestBody = RequestBody.Companion.create(json, mediaType);

                OkHttpUtils.sendOkHttpResponse(LocalData.getInstance().getIp()+"register", requestBody, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        System.out.println(e);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        final String data = response.body().string();
                        int state = 1;
                        try {
                            JSONObject jojbect = new JSONObject(data);
                            state = jojbect.getInt("errcode");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        Looper.prepare();

                        if(state == 2){
                            Toast.makeText(RegisterActivity.this, "Register failed", Toast.LENGTH_SHORT).show();
                            //System.out.println("errorcode" + state);
                        }
                        else if(state == 1){
                            Toast.makeText(RegisterActivity.this, "This username already existed", Toast.LENGTH_SHORT).show();
                        }
                        else{
                            Toast.makeText(RegisterActivity.this, "Register succeeded", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                            startActivity(intent);
                            //System.out.println("errorcode" + state);
                        }
                        Looper.loop();
                    }
                });

            }catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}